package jeye.servlet.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jeye.dao.EventDAO;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

public class DefaultServletAction implements ServletAction {
	
	private static final String ID_PARAM = "id";

	private static final String FRAMES_TYPE = "frames";

	private static final String TYPE_PARAM = "type";
	
	@Autowired
	private EventDAO eventDAO;

	public void processAction(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String type = request.getParameter(TYPE_PARAM);
		PrintWriter writer = new PrintWriter(response.getWriter());
		List<Long> result = new ArrayList<Long>(0);
		
		if(FRAMES_TYPE.equals(type)) {
			String id = request.getParameter(ID_PARAM);
			
			if(StringUtils.isNumeric(id)) {
				result = eventDAO.getEventsFramesIds(Long.valueOf(id));
			}
		}
		else {
			result = eventDAO.getEventsIds();
		}
		
		for(Long id : result) {
			writer.println(id);
		}
	}	
}
